import { Vec2 } from "cc";

export interface GestureConfig {
    // 判定为点击的最大移动距离
    clickThreshold?: number;
    // 判定为点击的最大持续时间(毫秒)
    clickTimeThreshold?: number;
}

export class GestureRecognizer {
    private startPos: Vec2 = new Vec2();
    private startTime: number = 0;
    private isDragging: boolean = false;
    
    private readonly clickThreshold: number;
    private readonly clickTimeThreshold: number;

    constructor(config: GestureConfig = {}) {
        this.clickThreshold = config.clickThreshold || 10;
        this.clickTimeThreshold = config.clickTimeThreshold || 200;
    }

    // 触摸开始时调用
    public touchStart(position: Vec2): void {
        this.startPos.set(position);
        this.startTime = Date.now();
        this.isDragging = false;
    }

    // 触摸移动时调用
    public touchMove(position: Vec2): boolean {
        if (this.isDragging) return true;
        
        const distance = Vec2.distance(this.startPos, position);
        if (distance > this.clickThreshold) {
            this.isDragging = true;
            return true;
        }
        return false;
    }

    // 触摸结束时调用
    public touchEnd(position: Vec2): boolean {
        const distance = Vec2.distance(this.startPos, position);
        const duration = Date.now() - this.startTime;
        
        // 返回是否是点击
        return !this.isDragging && 
               distance <= this.clickThreshold && 
               duration <= this.clickTimeThreshold;
    }

    // 重置状态
    public reset(): void {
        this.isDragging = false;
        this.startTime = 0;
        this.startPos.set(Vec2.ZERO);
    }
} 